package com.example.administrator.daggerdemo.utils;


import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2016/6/7.
 */
public class DateUtils {

    public static String dateFormat1="yyyy-MM-dd";
    public static String dateFormat2="MM月dd号";
    public static String dateFormat3="yyyy-MM-dd HH:mm:ss";
    public static String dateFormat4="yyyy年";
    public static String dateFormat5="HH:mm:ss";
    private static  SimpleDateFormat sdf;
    public static SimpleDateFormat getStandFormat(){
        if (sdf ==null)
        {
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
        }
        return sdf;
    }

    public static int getMonthMaxDay(String year,String month)
    {
        Calendar cal = Calendar.getInstance();
        cal.set(Integer.parseInt(year),Integer.parseInt(month),1);//获取到下个月一号
        cal.add(Calendar.DAY_OF_YEAR,-1);//本月最后一天
        return cal.get(Calendar.DAY_OF_MONTH);
    };

    /**
     * 获取格式日期
     * @param format
     * @return
     */
    public static String getNowTime(String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        return df.format(c.getTime());
    }



    /**
     * 获取格式日期
     * @param time
     * @param format
     * @return
     */
    public static String getNowTime(long time, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(time);
        return df.format(c.getTime());
    }

    public static String getNowTime(String time, String forformat,String tomat) {
        SimpleDateFormat df = new SimpleDateFormat(tomat);
        Calendar c = Calendar.getInstance();
        c.setTime(strToDate(time, forformat));
        return df.format(c.getTime());
    }
    /**
     * String 类型转long
     * @param time
     * @return
     */
    public static long strtolongDate(String time,String format) {
        Calendar c = Calendar.getInstance();
        c.setTime(strToDate(time, format));
        return c.getTimeInMillis();
    }
    public static Date strToDate(String strDate,String format) {//把格式是2012-12-02的日期串转为Date类型
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;

    }
    /**
     * 获取当天的前一天
     * @return
     */
    public static long getPreDay(String time,String format) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(strToDate(time,format));
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        return calendar.getTimeInMillis();
    }
    public static String getPreDay(String format) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        return getNowTime(calendar.getTimeInMillis(), format);
    }

    /**
     * 获取当天的前一天
     * @return
     */
    public static long getNextDay(String time,String format) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(strToDate(time, format));
        calendar.add(Calendar.DAY_OF_MONTH, +1);
        return calendar.getTimeInMillis();
    }


    public static Date getYesterDay(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR,-1);
        return  calendar.getTime();
    }

    public static Date getTomorrow(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR,1);
        return  calendar.getTime();
    }

    /**
     * 判断两个时间是不是在一天
     */
    public static boolean isOneDay(String time1,String time2){
        boolean isCommon = false;
        try {
            Date date1 = sdf.parse(time1);
            Date date2 = sdf.parse(time2);
            Calendar cal1 = Calendar.getInstance();
            Calendar cal2 = Calendar.getInstance();
            cal1.setTime(date1);
            cal2.setTime(date2);
            int year1 = cal1.get(Calendar.YEAR);
            int year2 = cal2.get(Calendar.YEAR);

            int dayOfYear1 = cal1.get(Calendar.DAY_OF_YEAR);
            int dayOfYear2 = cal2.get(Calendar.DAY_OF_YEAR);

            if (year1 == year2&&dayOfYear1 == dayOfYear2)
            {
                    isCommon = true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return isCommon;
    }

    public static boolean isOneDay(Date date1,Date date2){
        boolean isCommon = false;
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(date1);
        cal2.setTime(date2);
        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);

        int dayOfYear1 = cal1.get(Calendar.DAY_OF_YEAR);
        int dayOfYear2 = cal2.get(Calendar.DAY_OF_YEAR);

        if (year1 == year2&&dayOfYear1 == dayOfYear2)
        {
            isCommon = true;
        }
    return isCommon;
    }
}
